<?php

/**
 *    文章管理控制器
 *
 *    @author    Hyber
 *    @usage    none
 */
class WeiboApp extends BackendApp
{
	var $_weibo_mod;


	function __construct()
	{
		$this->WeiboApp();
	}

	function WeiboApp()
	{
		parent::BackendApp();
		$this->_weibo_mod =& m('weibo');
	}

	/**
	 *    文章索引
	 *
	 *    @author    Hyber
	 *    @return    void
	 */
	function index()
	{
			
		$conditions='';
		$conditions .= $this->_get_query_conditions(array(
		array(
                'field' => 'title',         //可搜索字段title
                'equal' => 'LIKE',          //等价关系,可以是LIKE, =, <, >, <>
                'assoc' => 'AND',           //关系类型,可以是AND, OR
                'name'  => 'title',         //GET的值的访问键名
                'type'  => 'string',        //GET的值的类型
		),
		));

		if(@$_GET['user']){
			$user=@$_GET['user'];
			$conditions.=" and cm.user_name like '%$user%' or  real_name like  '%$user%' "	;
		}
		$page   =   $this->_get_page(10);   //获取分页信息
		/*
		 $weibolist=$this->_weibo_mod->find(array(
		 'fields'   => ' * ',
		 'conditions'  => ' 1=1 ' . $conditions,
		 'limit'   => $page['limit'],
		 'order'   => ' wei_id DESC', //必须加别名
		 'count'   => true   //允许统计
		 ));    //找出所有的文章
		 */

		$sql="SELECT cw.*,cm.user_id,cm.user_name FROM cw_weibo cw
LEFT JOIN  cw_member cm on cw.uid=cm.user_id
WHERE 1=1 $conditions ORDER BY wei_id DESC limit $page[limit]";
		$weibolist=$this->_weibo_mod->db->getAll($sql);


		$sql="SELECT  count(1) FROM cw_weibo cw
LEFT JOIN  cw_member cm on cw.uid=cm.user_id
WHERE 1=1 ".$conditions;
		$count=$this->_weibo_mod->db->getOne($sql);

		$page['item_count']=$count;   //获取统计数据

		$this->_format_page($page);
		$this->import_resource(array('script' => 'inline_edit.js'));
		$this->assign('filtered', $conditions? 1 : 0); //是否有查询条件
		$this->assign('page_info', $page);   //将分页信息传递给视图，用于形成分页条
		$this->assign('weibolist', $weibolist);
		$this->display('weibo.index.html');
	}
	/**
	 *    新增文章
	 *
	 *    @author    Hyber
	 *    @return    void
	 */
	function add()
	{
		if (!IS_POST)
		{
			/* 显示新增表单 */

			$activity = array('sort_order' => 255);
			$this->assign("id", 0);
			$this->assign('activity', $activity);

			$sql="select  * from   cw_dictionary where  d_code='cat'";//
			$dis_list = $this->_activity_mod->GetAll($sql);
			$dis_cat=array();
			foreach ($dis_list as $k=>$v){
				$dis_cat[$v['d_key']]=$v['d_value'];
			}
			$this->import_resource(array('script' => 'jquery.plugins/jquery.validate.js,change_upload.js'));
			$this->assign('dis_cat', $dis_cat);
			$this->display('activity.form.html');
		}
		else
		{
			$data = array();
			$data['title']      =   $_POST['title'];
			$data['cat']    =   $_POST['cat'];
			$data['subject']    =   $_POST['subject'];
			$data['sort_order'] =   $_POST['sort_order'];
			$data['content'] =   $_POST['article_content'];

			if (!$article_id = $this->_activity_mod->add($data))  //获取article_id
			{
				$this->show_warning($this->_activity_mod->get_error());

				return;
			}

			/* 处理上传的图片 */
			$logo       =   $this->_upload_logo($article_id);
			if ($logo === false)
			{
				return;
			}
			$logo && $this->_activity_mod->edit($article_id, array('pic' => $logo)); //将logo地址记下

			$this->show_message('add_activity_successed',
                'back_list',    'index.php?app=activity',
                'continue_add', 'index.php?app=activity&amp;act=add'
                );
		}
	}
	/**
	 *    编辑文章
	 *
	 *    @author    Hyber
	 *    @return    void
	 */
	function edit()
	{
		$article_id = isset($_GET['id']) ? intval($_GET['id']) : 0;
		if (!$article_id)
		{
			$this->show_warning('no_such_weibo');
			return;
		}
		if (!IS_POST)
		{
			$article_id = isset($_GET['id']) ? intval($_GET['id']) : 0;
			if (!$article_id)
			{
				$this->show_warning('no_such_weibo');
				return;
			}//_weibo_mod
			$sql="SELECT cw.*,cm.user_id,cm.user_name FROM cw_weibo cw
LEFT JOIN  cw_member cm on cw.uid=cm.user_id
WHERE wei_id= ".$article_id;

			$weibo=$this->_weibo_mod->db->getRow($sql);
			$wei_pics='';
			$wei_pics= explode(',',$weibo['wei_pic']);
			$weibo['wei_pic']=$wei_pics;
			if (empty($weibo))
			{
				$this->show_warning('no_such_weibo');
				return;
			}

			$this->import_resource(array('script' => 'jquery.plugins/jquery.validate.js,change_upload.js'));

			$this->assign('weibo', $weibo);
			$this->display('weibo.rec.html');
		}
		else
		{
			$data = array();
			$data['title']      =   $_POST['title'];
			$data['rec']      =  $_POST['rec'];
			$data['content'] =   $_POST['content'];


			$rows=$this->_weibo_mod->edit($article_id, $data);
			if ($this->_weibo_mod->has_error())
			{
				$this->show_warning($this->_weibo_mod->get_error());

				return;
			}

			/* 处理上传的图片 */
			$logo       =   $this->_upload_logo($article_id);
			if ($logo === false)
			{
				return;
			}
			$logo && $this->_weibo_mod->edit($article_id, array('pic' => $logo)); //将logo地址记下

			$this->show_message('edit_activity_successed',
                'back_list',        'index.php?app=weibo',
                'edit_again',    'index.php?app=weibo&amp;act=edit&amp;id=' . $article_id);
		}
	}

	function view(){
		$article_id = isset($_GET['id']) ? intval($_GET['id']) : 0;
		if (!$article_id)
		{
			$this->show_warning('no_such_weibo');
			return;
		}//_weibo_mod
		$sql="SELECT cw.*,cm.user_id,cm.user_name FROM cw_weibo cw
LEFT JOIN  cw_member cm on cw.uid=cm.user_id
WHERE wei_id= ".$article_id;

		$weibo=$this->_weibo_mod->db->getRow($sql);
		$wei_pics='';
		$wei_pics= explode(',',$weibo['wei_pic']);
		$weibo['wei_pic']=$wei_pics;
		if (empty($weibo))
		{
			$this->show_warning('no_such_weibo');
			return;
		}
			
		$this->import_resource(array('script' => 'jquery.plugins/jquery.validate.js,change_upload.js'));
			
		$this->assign('weibo', $weibo);
		$this->display('weibo.form.html');

			
	}

	//异步修改数据
	function ajax_col()
	{
		$id     = empty($_GET['id']) ? 0 : intval($_GET['id']);
		$column = empty($_GET['column']) ? '' : trim($_GET['column']);
		$value  = isset($_GET['value']) ? trim($_GET['value']) : '';
		$data   = array();

		if (in_array($column ,array('rec')))
		{
			$data[$column] = $value;
			$this->_weibo_mod->edit($id, $data);
			if(!$this->_weibo_mod->has_error())
			{
				echo ecm_json_encode(true);
			}
		}
		else
		{
			return ;
		}
		return ;
	}
	function drop()
	{
		$article_ids = isset($_GET['id']) ? trim($_GET['id']) : '';
		if (!$article_ids)
		{
			$this->show_warning('no_such_weibo');

			return;
		}
		$article_ids=explode(',', $article_ids);
		$this->_weibo_mod->drop($article_ids);
		if ($this->_weibo_mod->has_error())    //删除
		{
			$this->show_warning($this->_weibo_mod->get_error());

			return;
		}

		$this->show_message('drop_ok');
	}

	function _upload_logo($brand_id)
	{
		$file = $_FILES['pic'];
		if ($file['error'] == UPLOAD_ERR_NO_FILE) // 没有文件被上传
		{
			return '';
		}
		import('uploader.lib');             //导入上传类
		$uploader = new Uploader();
		$uploader->allowed_type(IMAGE_FILE_TYPE); //限制文件类型
		$uploader->addFile($_FILES['pic']);//上传logo

		if (!$uploader->file_info())
		{
			$this->show_warning($uploader->get_error() , 'go_back', 'index.php?app=weibo&amp;act=edit&amp;id=' . $brand_id);
			return false;
		}
		// 指定保存位置的根目录
		$uploader->root_dir(ROOT_PATH);
		$imgname=gmtime();
		// 上传
		if ($file_path = $uploader->save('data/files/mall/weibo', $imgname))   //保存到指定目录，并以指定文件名$brand_id存储
		{
			return $file_path;
		}
		else
		{
			return false;
		}
	}




}

?>